package com.mlnx.web.device.controller;

import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.device.entity.Device;
import com.mlnx.device.entity.DeviceGroup;
import com.mlnx.device.enums.DeviceType;
import com.mlnx.device.pojo.dto.DeviceGroupDto;
import com.mlnx.device.pojo.form.ConfigEcgDeviceForm;
import com.mlnx.device.pojo.form.DeviceGroupForm;
import com.mlnx.device.pojo.form.DeviceGroupVOForm;
import com.mlnx.device.pojo.vo.DeviceGroupVO;
import com.mlnx.device.pojo.vo.DeviceGroupVO2;
import com.mlnx.device.service.DeviceGroupService;
import com.mlnx.device.service.DeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(value = "设备通用接口", tags = {"设备通用接口"})
@RestController
@RequestMapping("device")
public class DeviceController extends BaseController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceGroupService deviceGroupService;

    @ApiOperation(value = "获取设备信息")
    @GetMapping(value = "/{numeration}")
    public Response getDeviceInfo(@PathVariable("numeration") String numeration) {
        Device device = deviceService.getDevice(numeration);

        return result(device);
    }

    @ApiOperation(value = "获取患者绑定的设备")
    @GetMapping(value = "")
    public Response getBoundDevices(@RequestParam("patientId") Integer patientId) {

        return result(deviceService.getBoundDevices(patientId));
    }

    @ApiOperation(value = "获取设备数量")
    @GetMapping(value = "/count")
    public Response getDeviceCount() {

        return result(deviceService.getDeviceCount());
    }

    @ApiOperation(value = "获取各种类型设备信息")
    @GetMapping(value = "/type")
    public Response getTypeDevice(@RequestParam("typeId") DeviceType typeId, @RequestParam("pushId") Integer pushId,HttpServletRequest httpServletRequest) {
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));

        return result(deviceService.getTypeDevice(typeId, pushId,userId));
    }

    @ApiOperation(value = "配置心电设备")
    @GetMapping(value = "/config")
    public Response config(ConfigEcgDeviceForm configEcgDeviceForm) {
        deviceService.configEcgDevice(configEcgDeviceForm);
        return result();
    }

    @ApiOperation(value = "添加设备群组")
    @GetMapping(value = "group/add")
    public Response addGroup(DeviceGroupForm deviceGroupForm) {
        DeviceGroup deviceGroup =new DeviceGroup();
        BeanUtils.copyProperties(deviceGroupForm,deviceGroup);
        deviceGroupService.addGroup(deviceGroup);
        return result();
    }
    @ApiOperation(value = "群组中绑定设备")
    @GetMapping(value = "group/device")
    public Response addDeviceToGroup(DeviceGroupVOForm deviceGroupForm) {
        DeviceGroupVO deviceGroupVO =new DeviceGroupVO();
        BeanUtils.copyProperties(deviceGroupForm,deviceGroupVO);
        deviceGroupService.addDeviceToGroup(deviceGroupVO);
        return result();
    }
    @ApiOperation(value = "将病人与群组中设备绑定")
    @GetMapping(value = "group/patient")
    public Response boundPatient(@RequestParam("patientId") Integer patientId,@RequestParam("groupId") Integer groupId) {
        deviceService.bindDeviceByGroupId(patientId,groupId);
        return result();
    }

    @ApiOperation(value = "获得所有未绑定的设备")
    @GetMapping(value = "group/un/device")
    public ResponseData<List<Device>> getUnBindDevice() {
        return result( deviceService.getUnbindGroupDevice());
    }


    @ApiOperation(value = "将设备与设备群组解绑")
    @GetMapping(value = "group/remove")
    public Response  removeGroup(@RequestParam("deviceId") String deviceId) {
        deviceService.unbindDevice(deviceId);
        return result();
    }

    @ApiOperation(value = "获取设备群组")
    @GetMapping(value = "group/get/all")
    public ResponseData<List<DeviceGroupVO2>> getAllGroup() {

        return result(deviceService.getAllGroupInfo());
    }

    @ApiOperation(value = "根据手机MAC地址得到其他三个绑定设备的MAC和病人ID")
    @GetMapping(value = "group/phone/other")
    public ResponseData<DeviceGroupDto> getOtherDevice(@RequestParam("deviceId") String deviceId) {

        return result(deviceService.getOtherDevice(deviceId));
    }
}
